﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxCallback;

namespace TireService.Web.UserControls
{
    public partial class ucEditServiceNote : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void callbackEditServiceScheduleDetailNotePanel_Callback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            LoadUserControl(Convert.ToInt32(e.Parameter));
        }

        /// <summary>
        /// Public function for loading usercontrol
        /// </summary>
        public void LoadUserControl(int ServiceCarScheduleDetailId)
        {
            // Show usercontrol edit form
            pEditServiceScheduleDetailNote.Visible = true;

            // Initiate datalayer
            TireServiceDB db = new TireServiceDB();

            if (ServiceCarScheduleDetailId != 0)
            {
                LoadServiceCarScheduleDetailNotes(ServiceCarScheduleDetailId, db);
            }
            else
            {
                // Error
                throw new ArgumentException("Booking blev ikke fundet");
            }
        }

        /// <summary>
        /// Function that load booking and set edit form values
        /// </summary>
        /// <param name="ServiceCarScheduleDetailId">Id on booking</param>
        /// <param name="db">Connection to database</param>
        private void LoadServiceCarScheduleDetailNotes(int ServiceCarScheduleDetailId, TireServiceDB db)
        {
            // Fetch ServiceCarScheduleDetail
            ServiceCarScheduleDetail scheduledetail = db.GetServiceCarScheduleDetail(ServiceCarScheduleDetailId);

            if (scheduledetail == null)
            {
                // Error
                throw new ArgumentException("Servicekørsel blev ikke fundet");
            }

            if (scheduledetail.ScheduleDetailTypeId != (int)Shared.ScheduleDetailType.ServiceDrive)
            {
                // Error
                throw new ArgumentException("Noter kan ikke tilføjes til andet end service kørsler");
            }

            // Set values in edit form
            if (scheduledetail != null)
            {
                txtServiceScheduleDetailNote.Text = scheduledetail.Note;
                txtServiceScheduleDetailCustomerNote.Text = scheduledetail.CustomerNote;
            }
            else
            {
                // Error
                throw new ArgumentException("Hjulsæt kunne ikke loades");
            }
        }

        /// <summary>
        /// Callback function that saves tireset
        /// </summary>
        protected void callBackSaveServiceScheduleDetailNote_Callback(object sender, CallbackEventArgs e)
        {
            try
            {
                // Get values from client (0: ServiceCarScheduleDetailId, 1: Note, 2: CustomerNote)
                string[] param = e.Parameter.Split('|');

                int ServiceCarScheduleDetailId = Convert.ToInt32(param[0]);

                // Initiate datalayer
                TireServiceDB db = new TireServiceDB();

                ServiceCarScheduleDetail scheduledetail = db.GetServiceCarScheduleDetail(ServiceCarScheduleDetailId);

                if (scheduledetail == null)
                {
                    // Error
                    throw new ArgumentException("Servicekørsel blev ikke fundet");
                }

                scheduledetail.Note = Convert.ToString(param[1]); 
                scheduledetail.CustomerNote = Convert.ToString(param[2]);

                // Commit changes to database
                db.SubmitChanges();

                // Dispose database connection
                db.Dispose();
            }
            catch (Exception ex)
            {
                // Error
                throw new ArgumentException("Fejl opstod: " + ex.Message);
            }
        }
    }
}